<?php
abstract class BaseAttr {
	/**
	 * 当前表单属性的配置
	 * @var unknown_type
	 */
	var $attr = null;
	
	var $name = null;
	
	var $code = null;
	
	var $values = null;
	
	var $trait = null;
	
	var $required = false;
	
	var $desc = null;
	
	var $formHelper = null;
	
	/**
	 * 错误信息
	 * @var string
	 */
	var $error = null;
	
	/**
	 * 当前展示语言
	 * @var string
	 */
	var $lang = null;
	
	var $template_name = null;
	
	/**
	 * 当前显示表单所在的语言环境
	 * @var unknown_type
	 */
	var $mutil_lang = false;
	
	/**
	 * 
	 * @param array $attr 当前显示表单item的配置
	 * @param string $lang 当前展示的语言
	 * @param string $mutil_lang 是否是多语言里面展示，如果不为flase，则是语言名称，比如：zh_cn，在生成HTML时，必须使用语言名称，其它时候为true都可以
	 */
	public function BaseAttr($attr, $lang=null, $mutil_lang=null){
		if($lang == null){
			$this->lang = LANGUAGE;
		}
		
		if($mutil_lang!==null){
			$this->mutil_lang = $mutil_lang;
		}else{
			$this->mutil_lang = false;
		}
		
		if($mutil_lang!==false){
// 			$attr['value'] = $attr[$mutil_lang]['value'];
// 			$attr['name'] = $attr[$mutil_lang]['name'];
// 			$attr['desc'] = $attr[$mutil_lang]['desc'];
			lang_fatch($attr, $mutil_lang);
		}else{
			lang_fatch($attr, LANGUAGE);
// 			if(isset($attr[LANGUAGE]['name'])){
// 				$attr['name'] = $attr[LANGUAGE]['name'];
// 				$attr['desc'] = $attr[LANGUAGE]['desc'];
// 			}
		}
		$this->attr = &$attr;
		$this->name = &$attr['name'];
		$this->desc = &$attr['desc'];
		if(isset($attr['value'])){
			$this->values = &$attr['value'];
		}
		if(isset($attr['trait'])){
			$this->trait = &$attr['trait'];
		}
		self::init_template_name();
	}
	
	/**
	 * 初始化模板名称
	 */
	private function init_template_name(){
		if(empty($this->trait)){
			$this->template_name = 'normal.' . TEMPLATE_TYPE;
		}else{
			$this->template_name = $this->trait . '.' . TEMPLATE_TYPE;
		}
	}
	
	/**
	 * 私有获取数据的方法，一般在子类里面实现
	 */
	protected function init_data(){
		return null;
	}
	
	/**
	 * 获取表单代码
	 */
	public function get_html(){
		$template_path = self::get_template_path();
		if($template_path === false){
			return '找不到显示模板：' . $this->attr['type'] . '/' . $this->template_name;
		}
		$smarty = Viewer::get_view();
		$smarty->template_dir = $template_path;
		$this->attr['src_id'] = $this->attr['id']; //原始ID
		if($this->mutil_lang!==false){
			//如果启用了多语言编辑，则修改表单id
			$this->attr['id'] = $this->mutil_lang . '_' . $this->attr['id'];
			$this->attr['lang'] = $this->mutil_lang;
		}
		$smarty->assign('item', $this->attr);
// 		print_r($this->attr);
		
		//加载私有数据（Attr类内获取的数据）
		$private_data = $this->init_data();
		if($private_data!==null){
			$smarty->assign($private_data);
		}
		$html = $smarty->fetch($this->template_name);
		$smarty->free();
		return $html;
	}
	
	public function get_template_path(){
		//模板基本路径 
		$template_paths = array(
			ROOT_PATH . '/sites/main/' . MAIN_DOMAIN . '/template/data/forms/' . $this->attr['type'] . '/',	//私有目录
			ROOT_PATH . '/protected/includes/forms/template/' . $this->attr['type'] . '/',					//公共目录
		);
		
		foreach($template_paths as $template_path){
			if(is_file($template_path . $this->template_name)){
				//如果存在
				return $template_path;
			}
		}
		return false;
	}
	
	/**
	 * 获取Post数据
	 * @param array $getted_post_data 已经取到的表单数据
	 */
	public function get_post_val(&$getted_post_data){
		$name = $this->attr['id'];
		$require = $this->attr['required'];
		if($this->mutil_lang!==false){
			$data = array();
			foreach ($this->formHelper->langs as $lang){
				if(!isset($_POST[$lang . '_' . $name])) return false;
				$temp_data = $_POST[$lang . '_' . $name];
				$data[$lang] = $temp_data;
				$getted_post_data[$lang][$name] = $temp_data;
			}
		}else{
			if(!isset($_POST[$name])) return false;
			$data = $_POST[$name];
			$getted_post_data[$name] = $data; 
		}
		return $data;
	}
}
?>